﻿using DATAGRID实例MVVM.DB;
using DATAGRID实例MVVM.Models;
using DATAGRID实例MVVM.Views;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DATAGRID实例MVVM.ViewModel
{
    class MainViewModel:ViewModelBase
    {
        localDb localdb;

        private ObservableCollection<Student> gridModelList;

        public ObservableCollection<Student> GridModelList
        {
            get { return gridModelList; }
            set { gridModelList = value;RaisePropertyChanged(); }
        }

        #region Command
        public RelayCommand QueryCommand { set; get; }
        public RelayCommand ResetCommand { set; get; }
        public RelayCommand<int> EditCommand { set; get; }
        public RelayCommand<int> DelCommand { set; get; }
        public RelayCommand AddCommand { set; get; }
        #endregion


        private string search = string.Empty;

        public string Search
        {
            get { return search; }
            set { search = value; RaisePropertyChanged(); }
        }

        public MainViewModel()
        {
            localdb = new localDb();
            QueryCommand = new RelayCommand(Query);
            ResetCommand = new RelayCommand(() => {
                Search = string.Empty;
                this.Query();
            });

            EditCommand = new RelayCommand<int>(t => Edit(t));
            DelCommand = new RelayCommand<int>(new Action<int>(Del));
            AddCommand = new RelayCommand(Add);
        }

        public void Query()
        {
            var models = localdb.GetStudentByName(Search);
            GridModelList = new ObservableCollection<Student>();
            if(models!=null)
            {
                models.ForEach(arg=> {
                    GridModelList.Add(arg);
                });
            }
        }


        private void Del(int id)
        {
            var s = localdb.GetStudentById(id);
            if(s!=null)
            {
                var r=MessageBox.Show($"Are you sure to delete user:{s.Name}?", "Confirm dialogue", MessageBoxButton.OKCancel,MessageBoxImage.Question);
                if(r==MessageBoxResult.OK) localdb.DeleteStudentById(id);
                this.Query();
            }
        }

        private void Edit(int id)
        {
            var s=localdb.GetStudentById(id);
            
            if(s!=null)
            {
                UserView view = new UserView(s);
                var r=view.ShowDialog();
                if(r.Value)
                {
                   var olds= GridModelList.FirstOrDefault(t => t.Id == s.Id);
                    if (olds != null)
                    {
                        olds.Name = s.Name;
                    }
                }
            }
        }

        public void Add()
        {
            Student s = new Student();
            UserView view = new UserView(s);
            var r = view.ShowDialog();
            if (r.Value)
            {
                 localdb.AddStudent(s);
                Query();
            }
        }

    }
}
